package net.piencak.phonetools.eurocoins;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class EuroCoinCollector extends MIDlet implements CommandListener {

	private Command prev;
	private Command next;
	public Command exit;
	public Command coll;

	private CoinCommand[] selectCoin = new CoinCommand[Gallery.COIN_NAME.length];

	private CoinForm coinForm;

	private int country = 1;
	private int coin = 0;

	/**
	 * Creates several screens and navigates between them.
	 */
	public EuroCoinCollector() {
		// this.manager = new DisplayManager(Display.getDisplay(this));
		next = new Command("->", Command.SCREEN, 1);
		prev = new Command("<-", Command.SCREEN, 1);
		exit = new Command("Koniec", Command.SCREEN, 5);
		coll = new Command("Zbierka", Command.SCREEN, 1);

		coinForm = new CoinForm(this);
		coinForm.addCommand(next);
		coinForm.addCommand(prev);
		coinForm.addCommand(exit);
		for (int i = 0; i < Gallery.COINS_COUNT; i++) {
			selectCoin[i] = new CoinCommand(i, 3);
			coinForm.addCommand(selectCoin[i]);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		Display.getDisplay(this).setCurrent(coinForm);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.microedition.lcdui.CommandListener#commandAction(javax.microedition
	 * .lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction(Command command, Displayable displayable) {
		if (command == next) {
			country++;
			if (country >= Gallery.COUNTRIES_COUNT + 1) {
				country = 1;
			}
			coinForm.setCoin(country, coin);
		} else if (command == prev) {
			country--;
			if (country <= 0) {
				country = Gallery.COUNTRIES_COUNT;
			}
			coinForm.setCoin(country, coin);
		} else if (command == exit) {
			coinForm.save();
			notifyDestroyed();
		} else if (command instanceof CoinCommand) {
			coin = ((CoinCommand) command).getIndex();
			coinForm.setCoin(country, coin);
		} else if (command == coll) {
			Display.getDisplay(this).setCurrent(coinForm);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
		coinForm.destroy();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {
		coinForm.save();
	}

	public void displayAlert(String title, String message, int timeout) {
		Display display = Display.getDisplay(this);

		Alert alert = new Alert(title);
		String[] alertString = { message };
		alert.setTimeout(timeout);

		// Add an image to Alert
//		if (display.numColors() > 2) {
			/*
			String icon = (display.isColor()) ? "/JavaPowered-8.png"
					: "/JavaPowered-2.png";
			try {
				Image image = Image.createImage(icon);
				if (image != null) {
					myAlert.setImage(image);
					System.out.println("Image created and added to alert.. ");
				} else {
					System.out.println("No Image created... ");
				}
				*/
				// Add string to Alert
				for (int i = 0; i < alertString.length; i++) {
					alert.setString(alertString[i]);
				}

				if (alert != null) {
					display.setCurrent(alert);
				}
				/*
			} catch (Exception e) {
				System.out.println("Exception in CreateImage() ");
			}
			*/
//		}
	}
}
